﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reactive.Disposables;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using Codeplex.Reactive;
using Codeplex.Reactive.Extensions;

namespace Codeplex.KinkumaFramework
{
    public class ReactivePropertyErrorsChecker : IObservable<bool>, IDisposable
    {
        private Subject<bool> source = new Subject<bool>();
        private IObservable<bool> errors;
        private Dictionary<int, bool> cache = new Dictionary<int, bool>();

        public ReactivePropertyErrorsChecker(params IObservable<object>[] sources)
        {
            this.errors = sources.Aggregate(
                true, 
                (xs, ys) => xs.CombineLatest(ys, (x, y) => x == null && y == null));
        }

        public IDisposable Subscribe(IObserver<bool> observer)
        {
            return source.Subscribe(observer);
        }

        public void Dispose()
        {
            if (disposable.IsDisposed)
            {
                return;
            }

            this.disposable.Dispose();
            this.source.OnCompleted();
            this.source.Dispose();
        }
    }
}
